<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\middleware\api\Auth;
use app\middleware\api\CheckLogin;
use app\model\Comments;
use app\model\Order;
class Comment extends BaseController
{
    protected $middleware=[
        CheckLogin::class=>['only'=>['index']]
    ];

    /**
     * 评论商品
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     */
    public function index()
    {
        (new \app\validate\api\Comment())->goCheck();
        $user=\request()->userid;
        $params=\request()->param();
        $params['users_id']=$user;

        $model=Comments::find(Comments::comment($params['product_id'],$params));
        $order=Order::find($model->order_id);

        if($order->comment <= 0){
            $order->comment=Order::BOOKING;
            $order->save();
        }
        return $this->success();
    }

    /**
     * 评论列表
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($limit=10)
    {
        (new \app\validate\api\Comment())->goCheck('list');
        return $this->success(Comments::getList(request()->param('product_id'),$limit));
    }
}
